#include <iostream>
#include <string>
#include <sw/redis++/redis++.h>
#include <gflags/gflags.h>
#include <thread>
#include "data_redis.hpp"

typedef sw::redis::Redis redis_t;

DEFINE_string(ip, "127.0.0.1", "这是服务器的IP地址, 格式: 127.0.0.1");
DEFINE_int32(port, 6379, "这个是服务器的端口， 格式： 6379");
DEFINE_int32(db, 0, "库的编号， 默认为0号");
DEFINE_bool(keep_alive, true, "是否进行长连接保活");


void session_test(const std::shared_ptr<ChatSystem::redis_t>& client)
{
    ChatSystem::Session sclient(client);
    sclient.append("sid1", "uid1");
    sclient.append("sid2", "uid2");

    sclient.remove("sid1");

    auto res1 = sclient.uid("sid1");
    if(res1)
    {
        std::cout << res1.value() << std::endl;
    }

    auto res2 = sclient.uid("sid2");
    if(res2)
    {
        std::cout << res2.value() << std::endl;
    }
    

}


void status_test(const std::shared_ptr<ChatSystem::redis_t>& client)
{
    ChatSystem::Status sclient(client);
    sclient.append("uid1");
    sclient.append("uid2");

    sclient.remove("uid1");

    std::cout << sclient.exists("uid2") << std::endl;

}

void code_test(const std::shared_ptr<ChatSystem::redis_t>& client)
{
    ChatSystem::Codes cclient(client);
    cclient.append("cid1", "1234", std::chrono::seconds(10));
    cclient.append("cid2", "5678", std::chrono::seconds(20));
    cclient.append("cid3", "3456", std::chrono::seconds(60));

    std::this_thread::sleep_for(std::chrono::seconds(10));

    cclient.remove("cid3");
    
    auto res3 = cclient.code("cid1");
    if(res3)
    {
        std::cout << res3.value() << std::endl;
    }
    auto res1 = cclient.code("cid2");
    if(res1)
    {
        std::cout << res1.value() << std::endl;
    }
    auto res2 = cclient.code("cid3");
    if(res2)
    {
        std::cout << res2.value() << std::endl;
    }
    
}




int main(int argc, char* argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);

    auto client = ChatSystem::RedisClientFactory::creat(FLAGS_ip, FLAGS_port, FLAGS_db, FLAGS_keep_alive);

    session_test(client);
    std::cout << "------------------" << std::endl;
    status_test(client);
    std::cout << "------------------" << std::endl;
    code_test(client);

}